package com.xbongbong.pro.statistic.service.impl;

import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.enums.DelEnum;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.BaseVO;
import com.xbongbong.pro.domain.entity.ChartCategoryTextEntity;
import com.xbongbong.pro.enums.errorcodes.StatisticsServicesErrorCodeEnum;
import com.xbongbong.pro.statistic.constant.ChartConstant;
import com.xbongbong.pro.statistic.model.ChartCategoryTextModel;
import com.xbongbong.pro.statistic.pojo.dto.ChartCategoryTextUpdateBatchDTO;
import com.xbongbong.pro.statistic.service.ChartCategoryTextService;
import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;


/**
 * @ClassName ChartCategoryTextServiceImpl
 * @Description 文本组件
 * @Author mengna.Deng
 * @Date 2021/11/30 15:25
 * @Version 1.0
 */
@Service("chartCategoryTextService")
public class ChartCategoryTextServiceImpl implements ChartCategoryTextService {

    private static final Logger LOG = LoggerFactory.getLogger(ChartCategoryTextServiceImpl.class);
    @Resource
    private ChartCategoryTextModel chartCategoryTextModel;


    @Override
    public BaseVO updateBatch(ChartCategoryTextUpdateBatchDTO chartCategoryTextSaveDTO) throws XbbException {
        BaseVO baseVO = new BaseVO();
        String corpid = chartCategoryTextSaveDTO.getCorpid();
        Long categoryId = chartCategoryTextSaveDTO.getCategoryId();
        List<ChartCategoryTextEntity> textList = chartCategoryTextSaveDTO.getTextList();
        limit(corpid,categoryId,textList);
        chartCategoryTextModel.updateBatch(textList,corpid);
        return baseVO;
    }

    @Override
    public void limit(String corpid,Long categoryId,List<ChartCategoryTextEntity> textList) throws XbbException {
        // 查出现有的次仪表盘下的chartId集合
        HashMap<String,Object> textParam = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        textParam.put("corpid",corpid);
        textParam.put("del", DelEnum.NORMAL);
        textParam.put("categoryId",categoryId);
        textParam.put("columns","id");
        List<ChartCategoryTextEntity> list = chartCategoryTextModel.findEntitys(textParam);
        List<Long> textIds;
        if (CollectionUtils.isNotEmpty(list)){
            textIds = list.stream().map(e ->e.getId()).collect(Collectors.toList());
        }else {
            textIds = new ArrayList<>();
        }
        int insertSize = 0;
        if (CollectionUtils.isNotEmpty(textList)){
            for (ChartCategoryTextEntity entity : textList) {
                Long id = entity.getId();
                Integer del = entity.getDel();
                if (null == id){
                    insertSize ++;
                }else {
                    if (Objects.equals(del,DelEnum.DELETE.getDel())){
                        textIds.remove(id);
                    }else {
                        if (!textIds.contains(id)){
                            textIds.add(id);
                        }
                    }
                }

            }
        }
        int totalSize = insertSize + textIds.size();
        if (totalSize >  ChartConstant.TEXT_SIZE_MAX){
            throw new XbbException(StatisticsServicesErrorCodeEnum.API_ERROR_224079, StatisticsServicesErrorCodeEnum.API_ERROR_224079.getMsg(), ChartConstant.TEXT_SIZE_MAX);
        }
    }


}